package com.kunyi.web.service.file.application.api;

import com.kunyi.platform.tool.api.JsonResult;
import com.kunyi.platform.tool.util.KyAssertUtil;
import com.kunyi.web.service.file.core.handel.KunyiFileHandelContxt;
import com.kunyi.web.service.file.core.handel.KunyiRepository;
import com.kunyi.web.service.file.core.transform.FileTransform;
import com.kunyi.web.service.file.entity.domain.Upload;
import com.kunyi.web.service.file.service.IExportService;
import com.kunyi.web.service.file.service.IUploadService;
import com.kunyi.web.serviceapi.file.application.api.IFileClient;
import com.kunyi.web.serviceapi.file.entity.dto.DataExportDto;
import com.kunyi.web.serviceapi.file.entity.dto.FileInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName FileClientImpl
 * @Description TODO
 * @Author pengjunjun
 * @Date 2024/03/04
 * @Version 1.0
 **/
@RestController
public class FileClientImpl implements IFileClient {

    @Autowired
    private IExportService excelService;

    @Autowired
    private IUploadService uploadService;

    @Autowired
    private KunyiFileHandelContxt kunyiFileHandelContxt;

    @Override
    public JsonResult<String> export(DataExportDto dto) {
        return JsonResult.success(excelService.export(dto));
    }

    @Override
    public JsonResult<FileInfoDto> getFileInfo(String fileName) {
        return JsonResult.success(FileTransform.upload2FileInfo(uploadService.getByFileName(fileName)));
    }

    @Override
    public JsonResult<String> upload(MultipartFile file) {
        if (file.isEmpty()) {
            return JsonResult.error("请选择您要上传的文件");
        }
        //获得对应的处理类
        KunyiRepository kunyiRepository = kunyiFileHandelContxt.handel();
        String src = kunyiRepository.uploadMultipartFile(file);
        return JsonResult.success(src);
    }

    @Override
    public ResponseEntity<Resource> download(String fileName) throws IOException {
        Upload upload = uploadService.getByFileName(fileName);
        KyAssertUtil.notNull(upload,"没有该文件!");
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        KunyiRepository kunyiRepository = kunyiFileHandelContxt.handel();
        try {
            inputStream = kunyiRepository.readStream(upload.getName());
            outputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
            byte[] byteArray = outputStream.toByteArray();
            // 读取文件内容
            ByteArrayResource resource = new ByteArrayResource(byteArray);
            // 设置响应头
            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" +  java.net.URLEncoder.encode(upload.getName(),"UTF-8"));
            // 返回响应实体
            return ResponseEntity.ok()
                    .headers(headers)
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(resource);
        } finally {
            if (null != inputStream) {
                inputStream.close();
            }
            if (null != outputStream) {
                outputStream.close();
            }
        }
    }

}
